/**
 * 模拟从原生获取单个权限状态
 * @param {string} type 权限名称  status: [yes: 已授权 no: 未授权]
 */
function getPermissionStatus(type) {
  console.log("开始获取权限状态: ", type);
  let result;
  switch (type) {
    case "gps":
      result = {
        type,
        status: "no",
      };
      break;
    case "camera":
      result = {
        type,
        status: "no",
      };
      break;
    case "storage":
      result = {
        type,
        status: "no",
      };
      break;
    default:
      result = {
        type,
        status: "yes",
      };
      break;
  }

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(result);
    }, Math.ceil(Math.random() * 5) * 1000);
  });
}

/**
 * 模拟从原生申请单个权限的授权
 * @param {string} type 权限名称  status: [pass: 同意 refuse: 拒绝]
 */
function applyPermission(type) {
  console.log("开始申请权限: ", type);
  let result;
  switch (type) {
    case "storage":
      result = {
        type,
        status: "refuse",
      };
      break;
    default:
      result = {
        type,
        status: "pass",
      };
      break;
  }

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(result);
    }, Math.ceil(Math.random() * 5) * 1000);
  });
}

module.exports = {
  getPermissionStatus,
  applyPermission,
};
